/**
 * Object Popup
 */
ninejs.extend(ninejs, function( exports ) {
	var popup = {};

	popup.show = function( options ) {
		if ( !options.popupId ) {
			options.popupId = "popup";
		}
		if ( options.hasMask && !options.popupId ) {
			options.popupId = "mask";
		}

		popup.params = options;

		var cWidth = exports.BrowserEntry.getClientHeight(),				
			sHeight = exports.BrowserEntry.getScrollHeight(),
			popupElem, maskElem, maskStyle;

		// 添加遮罩
		if ( options.hasMask ) {
			maskElem = exports.getElemById(options.maskId);

			if ( maskElem ) {
				maskElem.style.display = "block";
			} else {
				maskElem = document.createElement("div");
				maskElem.id = options.maskId;
			
				document.body.appendChild( maskElem );
			}
			
			maskStyle = {
				"width": cWidth + "px",
				"height": sHeight + "px",
				"backgroundColor": "#000000",
				"opacity": "0.35",
				"filter": "alpha(opacity=35)",
				"position": "absolute",
				"left": "0px",
				"top": "0px",
				"zIndex": 99999
			};

			exports.extend(maskStyle, options.maskStyle);
			
			for ( var i in maskStyle ) {
				maskElem.style[i] = maskStyle[i];
			}
		}
		
		// 添加弹出框
		popupElem = exports.getElemById(options.popupId);

		if ( popupElem ) {
			popupElem.style.display = "block";
		} else {
			popupElem = document.createElement("div");			
			popupElem.id = options.popupId;
			popupElem.className = "popup";
			
			if ( options.renderTo ) {
				exports.getElemById(options.renderTo).appendChild( popupElem );
			} else {
				document.body.appendChild( popupElem );
			}
		}
		
		if ( options.popupStyle ) {
			for ( var i in options.popupStyle ) {
				popupElem.style[i] = options.popupStyle[i];	
			}
		}
		
		popupElem.innerHTML = options.html || "";
		popup.addEvents();
		popup.fixPosition({ maskId: options.maskId, popupId: options.popupId });

		return popup;
	};

	popup.hide = function( opts, v ) {
		var popElem = exports.getElemById(opts.popupId || "popup"),
			maskElem = exports.getElemById(opts.maskId || "mask");
		
		if ( popElem ) {
			if ( !v ) {
				//popElem.innerHTML = "";
				popElem.style.display = "none";					
			} else {
				popElem.parentNode && popElem.parentNode.removeChild(popElem);
			}
		}
		
		if ( maskElem ) {
			maskElem.style.display = "none";	
		}
	};
	
	popup.fixPosition = function( opts ) {
		var popElem = exports.getElemById(opts.popupId || "popup"),
			maskElem = exports.getElemById(opts.maskId || "mask"),
			st = exports.BrowserEntry.getScrollTop(),
			sh = exports.BrowserEntry.getScrollHeight(),
			cw = exports.BrowserEntry.getClientWidth(),
			ch = exports.BrowserEntry.getClientHeight(),
			oh;
		
		if ( popElem ) {
			oh = popElem.offsetHeight;
			if ( ch > oh ) {
				popElem.style.top = (st + (ch - oh)/2) + "px";
			} else {
				popElem.style.top = st + "px";
			}
		}
		
		if ( maskElem ) {
			maskElem.style.width = cw + "px";
			maskElem.style.height = sh + "px";
		}

		window.onscroll = function() {
			var params = popup.params;
			popup.fixPosition({ popupId: params.popupId, maskId: params.maskId });
		};
		window.onresize = function() {
			var params = popup.params;
			popup.fixPosition({ popupId: params.popupId, maskId: params.maskId });
		};
	};

	popup.addEvents = function() {
		var events = popup.params.events;
		for ( var i = 0, len = events.length; i < len; i++ ) {
			(function( i ) {
				var elem = document.getElementById(events[i].id);
				elem && exports.EventEntry.addEvent( elem, events[i].ev, events[i].fn );
			})( i );
		}
	};

	exports.Popup = popup;
});